<template>
  <div>
    <el-form labelWidth="120px" :disabled="status==1">
      <el-form-item label="申请部门" class="is-required">
        <selectorg @getselectorgid="getselectorgid" :editorgid="editorgid"></selectorg>
        <div class="el-form-item__error" v-if="!$v.gooutData.branchid.required">
          请选择申请部门
        </div>
      </el-form-item>
      <gettimelen :lasttimearr.sync="lasttimearr" :timename="'申请时间'" :status="timestatus"></gettimelen>
      <el-form-item label="外出地点" class="is-required">
        <el-input v-model="gooutData.address" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.gooutData.address.required">
          请输入外出地点
        </div>
      </el-form-item>
      <el-form-item label="外出事由" class="is-required">
        <el-input v-model="gooutData.detail" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.gooutData.detail.required">
          请输入外出事由
        </div>
      </el-form-item>
      <el-form-item label="参加人员" class="is-required">
        <el-input v-model="gooutData.userdetail" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.gooutData.userdetail.required">
          请输入参加人员
        </div>
      </el-form-item>

      <el-form-item label="选择审批流" v-if="status!=1" class="is-required">
        <choosespl @getsplid="getsplid" :atypeid="'0105'"></choosespl>
        <div class="el-form-item__error" v-if="!$v.aid.required">
          请选择审批流
        </div>
      </el-form-item>
    </el-form>
    <div style="text-align:right" v-if="status==0">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitgoout">确 定</el-button>
    </div>
    <div style="text-align:right" v-if="status==2">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitgoout2">重新提交</el-button>
    </div>
  </div>
</template>

<script>
import selectorg from '~/components/common/selectorg.vue';
import choosespl from '~/components/apply/choosespl.vue';
import gettimelen from '~/components/common/gettimelen.vue';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    aid: {
      required,
    },
    gooutData: {
      branchid: {
        required,
      },

      address: {
        required,
      },
      detail: {
        required,
      },

      userdetail: {
        required,
      },
    },
  },
  data() {
    return {
      flag: false,
      dialogVisible: true,
      ts: this.newStore(),
      timestatus: '',
      gooutData: {
        starttime: [],
        endtime: [],
        startinterval: [],
        endinterval: [],
        leavenum: '',
        leavedetailnum: [],
      },
      lasttimearr: [],
      aid: '',
      editorgid: '',
      options: [
        {
          value: '1',
          label: '上午',
        },
        {
          value: '2',
          label: '下午',
        },
      ],
    };
  },
  components: {
    selectorg,
    choosespl,
    gettimelen,
  },
  props: {
    status: Number,
    checkdetail: Object,
    fun: Function,
  },
  created() {
    this.timestatus = this.status;
    if (this.status != 0) {
      for (let i = 0; i < this.checkdetail.operation2.length; i += 1) {
        const b = {};
        b.starttime = this.checkdetail.operation2[i].hash.starttime;
        b.startinterval = this.checkdetail.operation2[i].hash.startinterval;
        b.endtime = this.checkdetail.operation2[i].hash.endtime;
        b.endinterval = this.checkdetail.operation2[i].hash.endinterval;
        b.timelen = this.checkdetail.operation2[i].hash.leavedetailnum;
        this.lasttimearr.push(b);
      }

   
        this.gooutData = this.checkdetail.operation1[0].hash;

      this.editorgid = this.gooutData.branchid;
      this.gooutData.starttime = [];
      this.gooutData.endtime = [];
      this.gooutData.startinterval = [];
      this.gooutData.endinterval = [];
      this.gooutData.leavenum = '';
      this.gooutData.leavedetailnum = [];
    }
  },
  methods: {
    checksplnum(money2, splid) {
      const req = {
        handle: '9000B8B8D347526960C5BAB1FB6894EF3277431D02D636A5015C1425899A1F59A41CF7A197311E4A',
        flowid: splid,
        number: money2,
      };
      const p = this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.data.operation1[0].hash.cou == '0') {
          this.flag = false;
        } else {
          this.flag = true;
        }
      });
      return p;
    },
    closedia() {
      this.$emit('closedia', false);
      this.$router.go(-1);
    },
    // 重新提交
    submitgoout2() {
      if (this.lasttimearr.length == 0) {
        this.$message({
          type: 'success',
          message: '请选择申请时间！',
        });
        return;
      }
      for (let i = 0; i < this.lasttimearr.length; i += 1) {
        if (this.lasttimearr[i].starttime == '' || this.lasttimearr[i].starttime == null) {
          this.$message({
            type: 'success',
            message: '请选择开始时间！',
          });
          return;
        }
        if (this.lasttimearr[i].startinterval == '' || this.lasttimearr[i].startinterval == null) {
          this.$message({
            type: 'success',
            message: '请选择开始时段！',
          });
          return;
        }
        if (this.lasttimearr[i].endtime == '' || this.lasttimearr[i].endtime == null) {
          this.$message({
            type: 'success',
            message: '请选择结束时间！',
          });
          return;
        }
        if (this.lasttimearr[i].endinterval == '' || this.lasttimearr[i].endinterval == null) {
          this.$message({
            type: 'success',
            message: '请选择结束时段！',
          });
          return;
        }
        this.gooutData.starttime.push(this.lasttimearr[i].starttime);
        this.gooutData.endtime.push(this.lasttimearr[i].endtime);
        this.gooutData.startinterval.push(this.lasttimearr[i].startinterval);
        this.gooutData.endinterval.push(this.lasttimearr[i].endinterval);
        this.gooutData.leavenum = Number(this.gooutData.leavenum) + Number(this.lasttimearr[i].timelen);
        this.gooutData.leavedetailnum.push(this.lasttimearr[i].timelen);
      }
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.gooutData.leavenum, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认重新提交外出会议、培训、学习申报审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle: '9000B8B8D347526960C5BAB1FB6894EF17427C100F77D58DB382894F762E66D209652941AB86E5AD',
                atype: '0105',
                flowid: this.aid,
                approvalid: this.gooutData.aid,
              };

              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.gooutData;

                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle = '06BBA4638D6221E7C94D5152B4CB8D877A1F05FDC4A221F3DA686A0A023C8D3B54347431A207BA75';
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '重新提交成功！',
                  });
                  this.fun();
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    submitgoout() {
      if (this.lasttimearr.length == 0) {
        this.$message({
          type: 'success',
          message: '请选择申请时间！',
        });
        return;
      }
      for (let i = 0; i < this.lasttimearr.length; i += 1) {
        if (this.lasttimearr[i].starttime == '' || this.lasttimearr[i].starttime == null) {
          this.$message({
            type: 'success',
            message: '请选择开始时间！',
          });
          return;
        }
        if (this.lasttimearr[i].startinterval == '' || this.lasttimearr[i].startinterval == null) {
          this.$message({
            type: 'success',
            message: '请选择开始时段！',
          });
          return;
        }
        if (this.lasttimearr[i].endtime == '' || this.lasttimearr[i].endtime == null) {
          this.$message({
            type: 'success',
            message: '请选择结束时间！',
          });
          return;
        }
        if (this.lasttimearr[i].endinterval == '' || this.lasttimearr[i].endinterval == null) {
          this.$message({
            type: 'success',
            message: '请选择结束时段！',
          });
          return;
        }
        this.gooutData.starttime.push(this.lasttimearr[i].starttime);
        this.gooutData.endtime.push(this.lasttimearr[i].endtime);
        this.gooutData.startinterval.push(this.lasttimearr[i].startinterval);
        this.gooutData.endinterval.push(this.lasttimearr[i].endinterval);
        this.gooutData.leavenum = Number(this.gooutData.leavenum) + Number(this.lasttimearr[i].timelen);
        this.gooutData.leavedetailnum.push(this.lasttimearr[i].timelen);
      }
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.gooutData.leavenum, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认提交外出会议、培训、学习申报审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle: '9000B8B8D347526960C5BAB1FB6894EF8B7B312373E9BEF47A8517CE4098C19A',
                atype: '0105',
                flowid: this.aid,
              };
              console.log(req, '789');
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.gooutData;

                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle = '06BBA4638D6221E7C94D5152B4CB8D877A1F05FDC4A221F34B73C71EED1C1BDA88D6D925C779BA4C';
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '发起外出会议、培训、学习申报审批成功！',
                  });
                  this.$emit('closedia', false);
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    datedifference(sDate1, sDate2) {
      // sDate1和sDate2是2006-12-18格式
      let dateSpan;
      let iDays;
      sDate1 = Date.parse(sDate1);
      sDate2 = Date.parse(sDate2);
      dateSpan = sDate2 - sDate1;
      dateSpan = Math.abs(dateSpan);
      iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
      return iDays;
    },
    // 获取部门
    getselectorgid(val) {
      // console.log(val, "0909");
      this.gooutData.branchid = val;
    },
    getsplid(val) {
      this.aid = val;
    },
  },
};
</script>

<style>
</style>
